package com.antgroup.seckill.business.goods.controller;

import com.antgroup.seckill.business.common.vo.request.UserLoginRequest;
import com.antgroup.seckill.business.common.vo.response.RespBean;
import com.antgroup.seckill.business.goods.service.IUserService;
import com.antgroup.seckill.business.utils.MD5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * @author wangxy
 * @date 2024/7/25 21:59
 */
@Controller
@RequestMapping("/washingShoes")
@Slf4j
public class LoginController {

    @Autowired
    IUserService iUserService;

    /**
     * 跳转到登录页面
     *
     * @return
     */
    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    /**
     * 用户登录
     *
     * @return
     */
    @PostMapping("/doLogin")
    @ResponseBody
    public RespBean doLogin(@RequestBody @Valid UserLoginRequest userLoginRequest, HttpServletRequest request,
                            HttpServletResponse response) {
        //模拟前端用户密码加密传给后端
        String inputPassToFromPass = MD5Util.inputPassToFromPass(userLoginRequest.getPassword());
        userLoginRequest.setPassword(inputPassToFromPass);
        RespBean resp = iUserService.doLogin(userLoginRequest, request, response);
        return resp;
    }

}
